var app = angular.module('zhijiaoAdminApp.filters' , []);



app.filter('windowConfig' , function(){
	return function(data , config){
		if(!window[config]){
			return '';
		}
		var getString = function(id){
			for(i in window[config]){
				var data = window[config][i];
				if(data['id'] == id)
					return data['name'];
			}
		}
		if(isNaN(data)){
			data = typeof data == 'string' ? JSON.parse(data) : data;
			var string = [];
			data.forEach(id=>{
				string.push(getString(id));
			})
			return string.join('、');
		}else{
			return getString(data);
		}
		return '';
	}
})

//返回距离当前时间多少天
app.filter('timeToDayNum' , function(){
	return function(time , str){
		str = str || '天';
		var nowTime = new Date().getTime() / 1000;
		if(nowTime < time){
			return -1;
		}
		if(nowTime - time < 3600 * 24)
			return '今天';
		var day = Math.ceil((nowTime - time) / 3600 / 24);
		return day >= 100 ? '99+' : day+str;
	}
})

app.filter('filterLabel' , function(){
	return function(arr){
		var label = [];
		if(arr){
			arr.forEach(item => {
				label.push(item.name);
			})
		}
		var string = label.join(',');
		console.log(string)
		return string;
	}
})

app.filter('city' , function(){
	return function(city){
		if(typeof city == 'string'){
			var city = JSON.parse(city);
		}
		var str = ChineseDistricts[86][city[0]];
		str = str + ' ' + ChineseDistricts[city[0]][city[1]];
		str = str + ' ' + ChineseDistricts[city[1]][city[2]];
		return str;
	}
})

app.filter('getDataName' , function(){
	return function(id , arr){
		for(var i in arr){
			if(arr[i].id == id){
				return arr[i].name;
			}
		}
		return '';
	}
})